package com.ourteam.frame.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public abstract class AbstractTreeBuilderHelper<T extends AbstractTreeBuilder> {

	public static final int ALL_LEVEL = -1;

	public static final int ROOT_LEVEL = 0;

	private T treeBuilder;

	public AbstractTreeBuilderHelper(T treeBuilder) {
		super();
		this.treeBuilder = treeBuilder;
	}

	abstract protected Object getInitParameter();

	public GridBean buildTree(TreeParamModel treeParamModel) throws Exception {

		return buildTree(treeParamModel, ALL_LEVEL);
	}

	public GridBean buildTree(TreeParamModel treeParamModel, int expandLevel) throws Exception {

		int nodeLevel = 0;

		if (StringUtils.isNotEmpty(treeParamModel.getN_level())) {
			nodeLevel = Integer.parseInt(treeParamModel.getN_level());
		}

		String parentObj = treeParamModel.getNodeid();

		if (parentObj == null && getInitParameter() != null) {
			parentObj = String.valueOf(getInitParameter());
		}

		List<TreeViewNodeBean> treeViewNodeBeans = new ArrayList<TreeViewNodeBean>();

		buildTreeNode(parentObj, nodeLevel, treeParamModel.getNodeConfigId(), treeViewNodeBeans, expandLevel);

		GridBean gridBean = new GridBean();

		gridBean.setRows(treeViewNodeBeans.toArray(new TreeViewNodeBean[treeViewNodeBeans.size()]));

		return gridBean;
	}

	protected void buildTreeNode(Object parentObj, int level, String nodeCinfigId, List<TreeViewNodeBean> treeNodes,
			int expandLevel) throws Exception {

		if (parentObj == null && getInitParameter() != null) {
			parentObj = String.valueOf(getInitParameter());
		}

		TreeViewNodeBean[] treeViewNodeBeans = treeBuilder.getTreeGrid(parentObj, level, nodeCinfigId);

		for (int i = 0; i < treeViewNodeBeans.length; i++) {

			TreeViewNodeBean treeViewNodeBean = treeViewNodeBeans[i];

			treeNodes.add(treeViewNodeBean);

			if ((expandLevel != ALL_LEVEL && level <= expandLevel && treeViewNodeBean.getIsLeaf() == false)
					|| (expandLevel == ALL_LEVEL && treeViewNodeBean.getIsLeaf() == false)) {
				buildTreeNode(treeViewNodeBean.getId(), level + 1, treeViewNodeBean.getNodeConfigId(), treeViewNodeBean,
						expandLevel);

				treeViewNodeBean.getState().setExpanded(true);
			}

		}

	}

	protected void buildTreeNode(Object parentObj, int level, String nodeCinfigId, TreeViewNodeBean parentTreeNode,
			int expandLevel) throws Exception {

		TreeViewNodeBean[] treeViewNodeBeans = treeBuilder.getTreeGrid(parentObj, level, nodeCinfigId);

		for (int i = 0; i < treeViewNodeBeans.length; i++) {

			TreeViewNodeBean treeViewNodeBean = treeViewNodeBeans[i];

			if ((expandLevel != ALL_LEVEL && level <= expandLevel && treeViewNodeBean.getIsLeaf() == false)
					|| (expandLevel == ALL_LEVEL && treeViewNodeBean.getIsLeaf() == false)) {

				buildTreeNode(treeViewNodeBean.getId(), level + 1, treeViewNodeBean.getNodeConfigId(), treeViewNodeBean,
						expandLevel);

				treeViewNodeBean.getState().setExpanded(true);
			}

		}

		parentTreeNode.setNodes(treeViewNodeBeans);
	}

}
